function [capacity_noma, power_allocation, fig_noma] = noma_analysis(EbN0_dB, num_users, power_ratios)
% NOMA多址接入技术分析
% 输入参数：
%   EbN0_dB - 信噪比范围 (dB)
%   num_users - 用户数量数组
%   power_ratios - 功率分配比例
% 输出参数：
%   capacity_noma - NOMA容量
%   power_allocation - 功率分配结果
%   fig_noma - 图形句柄

% 添加路径
addpath('../Common');
colors = color_definitions();

% 参数设置
bandwidth = 180e3; % 带宽 (Hz)
channel_gains = rand(num_users, 1); % 随机信道增益 (降序排列)
channel_gains = sort(channel_gains, 'descend');
noise_power_dBm = -174 + 10*log10(bandwidth);
noise_power = 10^((noise_power_dBm-30)/10);

% 初始化结果数组
num_snr = length(EbN0_dB);
num_user_cases = length(num_users);
num_power_cases = length(power_ratios);
capacity_noma = zeros(num_snr, num_user_cases);
power_allocation = zeros(num_user_cases, max(num_users));

fprintf('NOMA多址接入技术分析...\n');

% NOMA容量和功率分配分析
for snr_idx = 1:num_snr
    snr_linear = 10^(EbN0_dB(snr_idx)/10);
    total_power = snr_linear * noise_power;
    
    for user_idx = 1:num_user_cases
        K = num_users(user_idx);
        
        % SIC (串行干扰消除) 容量计算
        user_capacities = zeros(K, 1);
        
        % 为每个用户计算容量 (考虑SIC)
        for k = 1:K
            % 用户k的信道增益
            h_k = channel_gains(k);
            
            % 用户k的功率分配
            if k <= num_power_cases
                power_k = power_ratios(k) * total_power;
            else
                power_k = total_power / K; % 平均分配
            end
            
            % 计算干扰 (来自信道增益更差的用户)
            interference_power = 0;
            for j = (k+1):K
                if j <= num_power_cases
                    interference_power = interference_power + power_ratios(j) * total_power * channel_gains(j);
                else
                    interference_power = interference_power + (total_power/K) * channel_gains(j);
                end
            end
            
            % 用户k的SINR
            sinr_k = power_k * h_k / (noise_power + interference_power);
            
            % Shannon容量
            if sinr_k > 0
                user_capacities(k) = bandwidth * log2(1 + sinr_k);
            end
        end
        
        % 总容量
        capacity_noma(snr_idx, user_idx) = sum(user_capacities);
        
        % 保存功率分配结果 (最后一次迭代)
        if snr_idx == num_snr
            for k = 1:K
                if k <= num_power_cases
                    power_allocation(user_idx, k) = power_ratios(k);
                else
                    power_allocation(user_idx, k) = 1/K;
                end
            end
        end
    end
end

%% 可视化结果
fig_noma = figure('Name', 'NOMA多址接入技术分析', 'Position', [500, 100, 1200, 800]);

% 子图1: NOMA容量 vs SNR
subplot(2, 2, 1);
for user_idx = 1:num_user_cases
    plot(EbN0_dB, capacity_noma(:, user_idx)/1e6, 'LineWidth', 2, 'Color', colors(user_idx, :));
    hold on;
end
grid on;
xlabel('Eb/N0 (dB)');
ylabel('容量 (Mbps)');
title('NOMA容量 vs 信噪比');
legend(arrayfun(@(x) sprintf('%d用户', x), num_users, 'UniformOutput', false), 'Location', 'NorthWest');

% 子图2: 功率分配
subplot(2, 2, 2);
user_fixed_idx = min(3, num_user_cases);
bar(1:num_users(user_fixed_idx), power_allocation(user_fixed_idx, 1:num_users(user_fixed_idx)), ...
    'FaceColor', colors(2, :));
xlabel('用户索引');
ylabel('功率分配比例');
title(sprintf('NOMA功率分配 (%d用户)', num_users(user_fixed_idx)));

% 子图3: 用户公平性
subplot(2, 2, 3);
% 计算用户间公平性 (Jain公平性指数)
fairness_index = zeros(num_user_cases, 1);
for user_idx = 1:num_user_cases
    K = num_users(user_idx);
    user_rates = zeros(K, 1);
    
    % 获取每个用户的速率 (使用中等SNR)
    snr_idx = round(num_snr/2);
    for k = 1:K
        h_k = channel_gains(k);
        
        if k <= num_power_cases
            power_k = power_ratios(k) * 10^(EbN0_dB(snr_idx)/10) * noise_power;
        else
            power_k = (10^(EbN0_dB(snr_idx)/10) * noise_power) / K;
        end
        
        % 简化的SINR计算
        sinr_k = power_k * h_k / noise_power;
        user_rates(k) = bandwidth * log2(1 + sinr_k);
    end
    
    % Jain公平性指数
    fairness_index(user_idx) = (sum(user_rates)^2) / (K * sum(user_rates.^2));
end

plot(num_users, fairness_index, 's-', 'LineWidth', 2, 'Color', colors(3, :));
grid on;
xlabel('用户数量');
ylabel('Jain公平性指数');
title('NOMA用户公平性');
ylim([0, 1]);

% 子图4: SIC错误传播影响
subplot(2, 2, 4);
% 分析SIC错误传播的影响
sic_error_rates = [0, 0.01, 0.05, 0.1]; % SIC错误率
capacity_with_errors = zeros(length(sic_error_rates), 1);

user_fixed = num_users(user_fixed_idx);
total_capacity_ideal = capacity_noma(snr_fixed_idx, user_fixed_idx);

for err_idx = 1:length(sic_error_rates)
    error_rate = sic_error_rates(err_idx);
    
    % 简化的错误传播模型
    error_propagation_factor = 1 - error_rate * (user_fixed - 1);
    capacity_with_errors(err_idx) = total_capacity_ideal * max(0, error_propagation_factor);
end

bar(1:length(sic_error_rates), capacity_with_errors/1e6, 'FaceColor', colors(4, :));
set(gca, 'XTick', 1:length(sic_error_rates), 'XTickLabel', arrayfun(@(x) sprintf('%.0f%%', x*100), sic_error_rates, 'UniformOutput', false));
xlabel('SIC错误率');
ylabel('容量 (Mbps)');
title('NOMA SIC错误传播影响');

fprintf('NOMA分析完成！\n');

end